/**
 * 修复施工队伍审核状态和订单状态的业务逻辑
 * 区分 auditStatus (审核状态) 和 status (订单状态)
 */

const fixAuditStatusLogic = () => {
  console.log('🔧 修复施工队伍状态字段业务逻辑...')
  
  console.log('\n📋 字段说明:')
  console.log('🔍 auditStatus 字段: 表示审核是否通过')
  console.log('   - approved, passed, 通过, 审核通过 等 = 审核通过')
  console.log('   - pending, 待审核, 审核中 等 = 审核中')
  console.log('   - rejected, 拒绝, 未通过 等 = 审核未通过')
  
  console.log('\n📋 status 字段: 表示施工队伍的订单状态')
  console.log('   - busy = 当前有订单在进行中')
  console.log('   - free/idle/空闲 = 当前没有订单，可以接新订单')
  console.log('   - 其他状态 = 根据具体业务定义')
  
  console.log('\n🎯 修复要点:')
  console.log('1. 审核验证应该检查 auditStatus 字段，而不是 status')
  console.log('2. status=busy 不应该阻止接单，这只是表示当前状态')
  console.log('3. 只有 auditStatus 未通过才应该拒绝接单')
  
  console.log('\n✏️ 需要修改的逻辑:')
  console.log('修改前 (错误逻辑):')
  console.log('```javascript')
  console.log('// 错误：检查 status 字段来判断审核状态')
  console.log('const isApproved = approvedStatuses.includes(team.status)')
  console.log('if (!isApproved) {')
  console.log('  return { error: "审核状态为busy，尚未通过审核" }')
  console.log('}')
  console.log('```')
  
  console.log('\n修改后 (正确逻辑):')
  console.log('```javascript')
  console.log('// 正确：检查 auditStatus 字段来判断审核状态')
  console.log('const isAuditApproved = approvedStatuses.includes(team.auditStatus)')
  console.log('if (!isAuditApproved) {')
  console.log('  return { error: `审核状态为"${team.auditStatus}"，尚未通过审核` }')
  console.log('}')
  console.log('')
  console.log('// status 字段只用于显示当前订单状态，不影响接单权限')
  console.log('console.log(`当前订单状态: ${team.status === "busy" ? "繁忙" : "空闲"}`)')
  console.log('```')
  
  console.log('\n📝 修改文件位置:')
  console.log('文件: cloudfunctions/teamOrderManager/index.js')
  console.log('函数: getTeamInfo(openid)')
  console.log('行数: 约490-510行')
  
  console.log('\n🚀 具体修改步骤:')
  console.log('1. 将 team.status 改为 team.auditStatus 进行审核检查')
  console.log('2. 保留 team.status 用于显示订单状态信息')
  console.log('3. 更新错误消息，明确区分审核状态和订单状态')
  console.log('4. 重新部署云函数')
}

const showCodeChanges = () => {
  console.log('\n💻 代码修改示例:')
  
  console.log('\n// 第1步: 记录两个状态字段')
  console.log('console.log("📋 施工队伍信息:", {')
  console.log('  auditStatus: team.auditStatus,  // 审核状态')
  console.log('  status: team.status,            // 订单状态')
  console.log('})')
  
  console.log('\n// 第2步: 修改审核检查逻辑')
  console.log('const approvedAuditStatuses = [')
  console.log('  "approved", "passed", "通过", "审核通过", "active"')
  console.log(']')
  console.log('const isAuditApproved = approvedAuditStatuses.includes(team.auditStatus)')
  console.log('')
  console.log('if (!isAuditApproved && team.auditStatus) {')
  console.log('  return {')
  console.log('    success: false,')
  console.log('    error: `您的施工队伍审核状态为"${team.auditStatus}"，尚未通过审核`')
  console.log('  }')
  console.log('}')
  
  console.log('\n// 第3步: 添加订单状态说明（不影响接单）')
  console.log('console.log(`订单状态: ${team.status === "busy" ? "当前有订单" : "可接新订单"}`)')
}

// 运行修复分析
fixAuditStatusLogic()
showCodeChanges()

module.exports = { fixAuditStatusLogic } 